/**
 * jims
 */
package com.jims.master.modules.template.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.template.entity.TreatRecordItem;
import com.jims.master.modules.template.service.TreatRecordItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * TreatRecordItemController
 * @author zp
 * @version 2017-07-17
 */
@Controller
@RequestMapping(value = "${adminPath}/treatrecorditem/treatrecorditem/treatRecordItem")
public class TreatRecordItemController extends BaseController {

	@Autowired
	private TreatRecordItemService treatRecordItemService;
	
	@ModelAttribute
	public TreatRecordItem get(@RequestParam(required=false) String id) {
		TreatRecordItem entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = treatRecordItemService.get(id);
		}
		if (entity == null){
			entity = new TreatRecordItem();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(TreatRecordItem treatRecordItem, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TreatRecordItem> page = treatRecordItemService.findPage(new Page<TreatRecordItem>(request, response), treatRecordItem); 
		model.addAttribute("page", page);
		return "modules/treatrecorditem/treatrecorditem/treatRecordItemIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(TreatRecordItem treatRecord,String type, Model model) {
//		treatRecord.setApplyId(applyId);
		List<TreatRecordItem> treatRecordItemList = treatRecordItemService.findList(treatRecord);
		if(treatRecordItemList.size()>0){
			treatRecord = treatRecordItemList.get(0);
		}
		model.addAttribute("treatRecordItem", treatRecord);
		return  "modules/template/recordTemplate/recordProjectItemForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(TreatRecordItem treatRecordItem, Model model) {
	    StringData data=new StringData();
		int i=treatRecordItemService.save(treatRecordItem);
        if(i>0){
            data.setCode("success");
            data.setData("保存TreatRecordItem成功");
        }else{
            data.setCode("error");
            data.setData("保存TreatRecordItem失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(TreatRecordItem treatRecordItem) {
	    StringData data=new StringData();
		int i=treatRecordItemService.delete(treatRecordItem);
        if(i>0){
            data.setCode("success");
            data.setData("删除TreatRecordItem成功");
        }else{
            data.setCode("error");
            data.setData("删除TreatRecordItem失败");
        }
		return data;
	}

}